// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 表示各种弹窗标识
    showModel: false,
    user: null,
    emil: null,
    password: null,
    nav: '0',
    //表示是否打开登陆注册页
    oplogin: null,
    tapuser: false,
    //字数
    maxWord: 255,
    currentWord:0,
    //下拉框使用
    index: 0,
    array: ['1','2','3','4','5','6','7','8','9','10'],
  },
  // 提交表单，发送登录请求
  formSubmit: function (e) {
    if (!e.detail.value.emil || !e.detail.value.password) {
      wx.showToast({
        title: '请填写完整字段！',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    })
    var that = this;
    wx.request({
      url: 'http://localhost:8086/login',
      method: 'POST',
      data: {
        "email": e.detail.value.emil,
        "password": e.detail.value.password
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data != null && res.data != "") {
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            user: res.data,
          })
          that.back()
          wx.setStorageSync('user', res.data)
          return;
        } else {
          wx.showToast({
            title: '密码或邮箱错误',
            icon: 'error',
            duration: 1000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 发送请求，验证token
  token(token) {
    var that = this;
    wx.request({
      url: 'http://localhost:8086/token',
      method: 'GET',
      header: {
        'token': token
      },
      success(res) {
        if (res.data != null && res.data != "") {
          that.setData({
            user: res.data,
          })
          that.back()
          wx.setStorageSync('user', res.data)
          // 更新文本域字数
          that.setData({
            currentWord: that.data.user.describes.length
          })
          // 更新图片编号,先排倒序再遍历提高效率
          var arrimg = that.data.user.avatarurl.split('').reverse();
          arrimg.map((item,index)=>{
              if (item == '.') {
                var arri = []
                for(var i = 0;i<arrimg.length;i++){
                  if (arrimg[index+1+i] != '/') {
                    arri.push(arrimg[index+1+i])
                  }else{
                    break
                  }
                }
                that.setData({
                  index: arri.reverse().join('')-1
                })
              }
          })
          return;
        } else {
          that.setData({
            user: null
          })
          wx.showToast({
            title: 'token过期，请重新登录',
            icon: 'none',
            duration: 1500
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 发送请求，退出登录
  logout() {
    var that = this;
    wx.request({
      url: 'http://localhost:8086/logout',
      method: 'GET',
      header: {
        'token': wx.getStorageSync('user').token
      },
      success(res) {
        if (res.data == 1) {
          that.setData({
            user: null
          })
          var user = wx.getStorageSync('user')
          user.token = null;
          user.password = null;
          that.setData({
            password: null
          })
          wx.setStorageSync('user', user)
          console.log(wx.getStorageSync('user'));
          wx.showToast({
            title: '退登成功',
            icon: 'success',
            duration: 1000
          })
          return;
        } else {
          wx.showToast({
            title: '退登失败',
            icon: 'error',
            duration: 1000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 提交表单，发送注册请求
  formSingup: function (e) {
    if (!e.detail.value.singemil || !e.detail.value.password || !e.detail.value.singname || !e.detail.value.confirmword) {
      wx.showToast({
        title: '请输入完整字段！',
        icon: 'none',
        duration: 1500
      })
      return;
    } else if (e.detail.value.password != e.detail.value.confirmword) {
      wx.showToast({
        title: '密码不一致！',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    })
    var that = this;
    wx.request({
      url: 'http://localhost:8086/singup',
      method: 'POST',
      data: {
        "name": e.detail.value.singname,
        "email": e.detail.value.singemil,
        "password": e.detail.value.password
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log(res);
        if (res.data == '注册成功') {
          wx.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            emil: e.detail.value.singemil,
            nav: 0
          })
          return;
        } else if (res.data == '注册失败') {
          wx.showToast({
            title: '注册失败',
            icon: 'error',
            duration: 1000
          })
          return;
        } else if (res.data == '该邮箱已注册') {
          wx.showToast({
            title: '该邮箱已注册',
            icon: 'error',
            duration: 1000
          })
          return;
        } else if (res.data == '信息不完整') {
          wx.showToast({
            title: '信息不完整',
            icon: 'error',
            duration: 1000
          })
          return;
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 前往登录注册
  gologin: function () {
    this.setData({
      oplogin: 1
    })
  },
  // 切换账号
  Tapuser: function (){
    this.setData({
      tapuser: true
    })
  },
  // 返回
  back: function () {
    this.setData({
      oplogin: null,
      tapuser: false
    })
  },
  // 导航栏切换事件
  navtap(e) {
    this.setData({
      nav: e.target.dataset.nav
    })
  },
  // 控制显示弹窗
  eject: function (e) {
    this.setData({
      showModel: e.currentTarget.dataset.id
    })
  },
  // 点击返回按钮隐藏
  back1: function () {
    this.setData({
      showModel: false
    })
  },
  // 提交表单，发送编辑收货信息请求
  formreceiving: function (e) {
    // 如果没有修改信息
    if (e.detail.value.consignee == this.data.user.consignee &&
      e.detail.value.receivingip == this.data.user.receivingip &&
      e.detail.value.receivingcalls == this.data.user.receivingcalls) {
      this.back1()
      return;
    }
    // 如果有信息未填写
    if (!e.detail.value.consignee || !e.detail.value.receivingip || !e.detail.value.receivingcalls) {
      wx.showToast({
        title: '请填写完整字段！',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // 发送请求
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    })
    var that = this;
    wx.request({
      url: 'http://localhost:8086/receiving',
      method: 'POST',
      data: {
        "consignee": e.detail.value.consignee,
        "receivingip": e.detail.value.receivingip,
        "receivingcalls": e.detail.value.receivingcalls
      },
      header: {
        'content-type': 'application/json',
        'token': that.data.user.token
      },
      success(res) {
        console.log(res);
        if (res.data == 'token无效') {
          wx.showToast({
            title: 'token无效',
            icon: 'error',
            duration: 1000
          })
          return;
        } else if (res.data == '请填写完整字段') {
          wx.showToast({
            title: '请填写完整字段',
            icon: 'error',
            duration: 1000
          })
          return;
        } else if (res.data != null) {
          wx.showToast({
            title: '编辑成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            user: res.data,
          })
          wx.setStorageSync('user', res.data)
          that.back1()
          return;
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 提交表单，发送充值请求
  formcoins: function(e){
    if(!e.detail.value.addcoins){
      // 无需发送请求
      this.back1()
      return;
    }
    // 发送请求
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    })
    var that = this;
    wx.request({
      url: 'http://localhost:8086/recharge',
      method: 'POST',
      data: e.detail.value.addcoins,
      header: {
        'content-type': 'application/json',
        'token': that.data.user.token
      },
      success(res) {
        console.log(res);
        if (res.data == 'token无效') {
          wx.showToast({
            title: 'token无效',
            icon: 'error',
            duration: 1000
          })
          return;
        } else if (res.data != null) {
          wx.showToast({
            title: '充值成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            user: res.data,
          })
          wx.setStorageSync('user', res.data)
          that.back1()
          return;
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 提交表单，发送修改密码请求
  formruppass: function(e) {
    console.log(e.detail.value);
    // 缺字段
    if (!e.detail.value.oldpassword || !e.detail.value.newpassword || !e.detail.value.confirmpass){
      wx.showToast({
        title: '请填写完整字段！',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // 密码不一致
    if (e.detail.value.newpassword != e.detail.value.confirmpass) {
      wx.showToast({
        title: '密码不一致！',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // 发送请求
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    })
    var that = this;
    wx.request({
      url: 'http://localhost:8086/uppasswrod',
      method: 'POST',
      data: {
        "email" : that.data.user.email,
        "oldpassword" : e.detail.value.oldpassword,
        "newpassword" : e.detail.value.newpassword,
        "confirmpass" : e.detail.value.confirmpass
      },
      header: {
        'content-type': 'application/json',
        'token': that.data.user.token
      },
      success(res) {
        console.log(res);
        if (res.data != 1) {
          wx.showToast({
            title: res.data,
            icon: 'error',
            duration: 1000
          })
          return;
        } else if (res.data == 1) {
          that.setData({
            user: null
          })
          var user = wx.getStorageSync('user')
          user.token = null;
          user.password = null;
          that.setData({
            password: null
          })
          wx.setStorageSync('user', user)
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 1000
          })
          that.back1()
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 提交表单，发送修改用户基础信息请求
  formrupuser: function(e) {
    if (!e.detail.value.name || !e.detail.value.email) {
      wx.showToast({
        title: '请填写完整字段！',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // 发送请求
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
    })
    var that = this;
    wx.request({
      url: 'http://localhost:8086/updatauser',
      method: 'POST',
      data: {
        "email" : e.detail.value.email,
        "name" : e.detail.value.name,
        "avatarurl" : parseInt(e.detail.value.avatarurl)+1,
        "describes" : e.detail.value.describes
      },
      header: {
        'content-type': 'application/json',
        'token': that.data.user.token
      },
      success(res) {
        if (!res.data.name) {
          wx.showToast({
            title: res.data,
            icon: 'error',
            duration: 1000
          })
          return;
        } else if (res.data.name) {
          that.setData({
            user: res.data
          })
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 1000
          })
          that.back1()
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络异常',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },
  // 查看头像
  lookimg: function () {
    wx.previewImage({
      urls: [this.data.user.avatarurl],
      showmenu: true
    })
  },
  // 监控字数
  limitWord:function(e){
   var that = this;
   var value = e.detail.value;
   //解析字符串长度
   var wordLength = parseInt(value.length);
   if (that.data.maxWord < wordLength) {
     return ;
   }
   that.setData({
     currentWord: wordLength
   })
  },
  //确认下拉框
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.getStorageSync('user').token != null) {
      this.token(wx.getStorageSync('user').token);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})